package com.example.androidexample.ndk;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.androidexample.R;
import com.example.basic.base.BaseActivity;
import com.example.clib.ArrayOpLibProxy;
import com.example.clib.ExceptionOpLibProxy;
import com.example.clib.NativeDynamicLibProxy;
import com.example.clib.NativeStaticLibProxy;
import com.example.clib.ObjectOpLibProxy;
import com.example.clib.StringOpLibProxy;
import com.example.clib.model.TestModel;

import java.util.Arrays;

public class NDKExampleActivity extends BaseActivity {

    private static final String TAG = "NDKExampleActivity";

    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {

        @Override
        public void handleMessage(@NonNull Message msg) {
            if (msg.what == 1) {
                TextView exceptionTvEnvAddress = findViewById(R.id.exception_tv_env_address);
                exceptionTvEnvAddress.setText(String.format("%s\n%s", exceptionTvEnvAddress.getText(), msg.obj));
            }
        }
    };
    private ExceptionOpLibProxy exceptionOpLibProxy;
    private ImageView icBack;

    @Override
    protected void initOnCreate(@Nullable Bundle savedInstanceState) {
        icBack = findViewById(R.id.ic_back);
        icBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        TextView toolBarTitle = findViewById(R.id.tool_bar_title);
        toolBarTitle.setText("NDK相关基础知识点");

        initViewForStaticRegister();
        initViewForDynamicRegister();
        initStringOp();
        initArrayOp();
        initObjectOp();
        initException();
    }

    @Override
    protected int setLayoutXmlid() {
        return R.layout.activity_ndk_example;
    }

    @Override
    public String getTag() {
        return TAG;
    }

    private void initException() {
        exceptionOpLibProxy = new ExceptionOpLibProxy();

        TextView exceptionTvThrowFormJni = findViewById(R.id.exception_tv_throw_form_jni);
        exceptionTvThrowFormJni.setOnClickListener(v -> {
            try {
                exceptionOpLibProxy.throwExceptionFormJNI();
            } catch (NoSuchMethodException e) {
                Log.d(TAG, "onClick: " + e.getMessage());
            }
        });

        TextView exceptionTvThrow = findViewById(R.id.exception_tv_throw);
        exceptionTvThrow.setOnClickListener(v -> {
            try {
                exceptionOpLibProxy.throwException();
            } catch (Exception e) {
                Log.d(TAG, "onClick: " + e.getMessage());
            }
        });

        TextView exceptionTvEnvAddress = findViewById(R.id.exception_tv_env_address);
        exceptionTvEnvAddress.setText(String.format("当前线程Name(点击得到子线程Env Name):%s\n%s", Thread.currentThread().getName(), exceptionOpLibProxy.getCurrentEnvAddress()));
        exceptionTvEnvAddress.setOnClickListener(v -> new Thread() {
            @Override
            public void run() {
                ExceptionOpLibProxy exceptionOpLibProxy1 = new ExceptionOpLibProxy();
                String mes = exceptionOpLibProxy1.getCurrentEnvAddress();
                Message message = handler.obtainMessage(1, mes);
                handler.sendMessage(message);
            }
        }.start());
    }

    @SuppressLint({"DefaultLocale", "SetTextI18n"})
    private void initObjectOp() {
        ObjectOpLibProxy objectOpLibProxy = new ObjectOpLibProxy();
        TextView objectTvStaticByte = findViewById(R.id.object_tv_static_byte);
        objectTvStaticByte.setText(String.format("从TestModel中拿到静态byte字段值：%s", objectOpLibProxy.getTestModelStaticFiledByte()));

        TextView objectTvStaticShort = findViewById(R.id.object_tv_static_short);
        objectTvStaticShort.setText(String.format("从TestModel中拿到静态short字段值：%s", objectOpLibProxy.getTestModelStaticFiledShort()));

        TextView objectTvStaticInt = findViewById(R.id.object_tv_static_int);
        objectTvStaticInt.setText(String.format("从TestModel中拿到静态int字段值：%s", objectOpLibProxy.getTestModelStaticFiledInt()));

        TextView objectTvStaticLong = findViewById(R.id.object_tv_static_long);
        objectTvStaticLong.setText(String.format("从TestModel中拿到静态long字段值：%s", objectOpLibProxy.getTestModelStaticFiledLong()));

        TextView objectTvStaticFloat = findViewById(R.id.object_tv_static_float);
        objectTvStaticFloat.setText(String.format("从TestModel中拿到静态float字段值：%s", objectOpLibProxy.getTestModelStaticFiledFloat()));

        TextView objectTvStaticDouble = findViewById(R.id.object_tv_static_double);
        objectTvStaticDouble.setText(String.format("从TestModel中拿到静态double字段值：%s", objectOpLibProxy.getTestModelStaticFiledDouble()));

        TextView objectTvStaticChar = findViewById(R.id.object_tv_static_char);
        objectTvStaticChar.setText(String.format("从TestModel中拿到静态char字段值：%s", objectOpLibProxy.getTestModelStaticFiledChar()));

        TextView objectTvStaticBoolean = findViewById(R.id.object_tv_static_boolean);
        objectTvStaticBoolean.setText(String.format("从TestModel中拿到静态boolean字段值：%s", objectOpLibProxy.getTestModelStaticFiledBoolean()));

        TextView objectTvStaticModel2 = findViewById(R.id.object_tv_static_model2);
        objectTvStaticModel2.setText(String.format("从TestModel中拿到静态对象Model2字段值：%s", objectOpLibProxy.getTestModelStaticFiledModel2()));

        TextView objectTvStaticStringArray = findViewById(R.id.object_tv_static_string_array);
        objectTvStaticStringArray.setText(String.format("从TestModel中拿到静态String数组字段值：%s", Arrays.toString(objectOpLibProxy.getTestModelStaticFiledStringArray())));

        TextView objectTvStaticMethod = findViewById(R.id.object_tv_static_method);
        objectTvStaticMethod.setText(String.format("从TestModel中通过jni修改基本数据类型值之后调用静态方法：%s", objectOpLibProxy.getTestModelStaticMethod()));

        TestModel testModelObject = objectOpLibProxy.getTestModelObject();
        TextView objectTvConstructor = findViewById(R.id.object_tv_constructor);
        objectTvConstructor.setText(String.format("从TestModel中通过构造方法创建实例对象：%s", testModelObject));

        TextView objectTvGetInt = findViewById(R.id.object_tv_get_int);
        objectTvGetInt.setText(String.format("得到TestModel的实例对象中的普通int字段值：%d", objectOpLibProxy.getTestModelFiledInt(testModelObject)));

        TextView objectTvGetModel2 = findViewById(R.id.object_tv_get_model2);
        objectTvGetModel2.setText(String.format("得到TestModel的实例对象中的普通object字段值：%s", objectOpLibProxy.getTestModelFiledModel2(testModelObject)));

        TextView objectTvCallMethod = findViewById(R.id.object_tv_call_method);
        String testModelMethod = objectOpLibProxy.getTestModelMethod(testModelObject);
        if (testModelMethod == null) {
            Log.d(TAG, "initObjectOp: null");
        } else {
            objectTvCallMethod.setText(String.format("调用TestModel的实例对象中的普通方法：%s",testModelMethod ));
        }
        
    }

    private void initArrayOp() {
        ArrayOpLibProxy arrayOpLibProxy = new ArrayOpLibProxy();
        byte[] byteArray = new byte[]{1, 6, 9, 10, 45, 12, 34, 65, 8, 90, 19, 51, 67, 16, 39};
        TextView arrayTvByte = findViewById(R.id.array_tv_byte);
        arrayTvByte.setText(String.format("数组排序（byte）\n原数组"+Arrays.toString(byteArray)+"\n排序后%s", Arrays.toString(arrayOpLibProxy.byteSort(byteArray))));

        short[] shortArray = new short[]{10, 60, 90, 100, 450, 120, 340, 650, 80, 900, 190, 510, 670, 160, 390};
        TextView arrayTvShort = findViewById(R.id.array_tv_short);
        arrayTvShort.setText(String.format("数组排序（short）\n原数组"+Arrays.toString(shortArray)+"\n排序后%s", Arrays.toString(arrayOpLibProxy.shortSort(shortArray))));

        int[] intArray = new int[]{100, 600, 900, 1000, 4500, 1200, 3400, 6500, 800, 9000, 1900, 5100, 6700, 1600, 3900};
        TextView arrayTvInt = findViewById(R.id.array_tv_int);
        arrayTvInt.setText(String.format("数组排序（int）\n原数组"+Arrays.toString(intArray)+"\n排序后%s", Arrays.toString(arrayOpLibProxy.intSort(intArray))));

        long[] longArray = new long[]{1000, 6000, 9000, 10000, 45000, 12000, 34000, 65000, 8000, 90000, 19000, 51000, 67000, 16000, 39000};
        TextView arrayTvLong = findViewById(R.id.array_tv_long);
        arrayTvLong.setText(String.format("数组排序（long）\n原数组"+Arrays.toString(longArray)+"\n排序后%s", Arrays.toString(arrayOpLibProxy.longSort(longArray))));

        float[] floatArray = new float[]{100.3333f, 600.3333f, 900.3333f, 1000.3333f, 4500.3333f, 1200.3333f, 3400.3333f, 6500.3333f, 800.3333f, 9000.3333f, 1900.3333f, 5100.3333f, 6700.3333f,
                1600.3333f, 3900.3333f};
        TextView arrayTvFloat = findViewById(R.id.array_tv_float);
        arrayTvFloat.setText(String.format("数组排序（float）\n原数组"+Arrays.toString(floatArray)+"\n排序后%s", Arrays.toString(arrayOpLibProxy.floatSort(floatArray))));

        double[] doubleArray = new double[]{100.3333333333d, 600.3333333333d, 900.3333333333d, 1000.3333333333d, 4500.3333333333d, 1200.3333333333d,
                3400.3333333333d, 6500.3333333333d, 800.3333333333d, 9000.3333333333d, 1900.3333333333d, 5100.3333333333d, 6700.3333333333d,
                1600.3333333333d, 3900.3333333333d};
        TextView arrayTvDouble = findViewById(R.id.array_tv_double);
        arrayTvDouble.setText(String.format("数组排序（double）\n原数组"+Arrays.toString(doubleArray)+"\n排序后%s", Arrays.toString(arrayOpLibProxy.doubleSort(doubleArray))));

        boolean[] booleanArray = new boolean[]{true, false, false, true, false, true, false, true, false, true, false, true, false, true, true};
        TextView arrayTvBoolean = findViewById(R.id.array_tv_boolean);
        arrayTvBoolean.setText(String.format("数组排序（boolean）\n原数组"+Arrays.toString(booleanArray)+"\n排序后%s", Arrays.toString(arrayOpLibProxy.booleanSort(booleanArray))));

    }

    private void initStringOp() {
        StringOpLibProxy stringOpLibProxy = new StringOpLibProxy();
        TextView stringTvStrSplicing = findViewById(R.id.string_tv_strSplicing);
        stringTvStrSplicing.setText(String.format("字符拼接 传入字符 测试数据 > %s", stringOpLibProxy.strSplicing("测试数据")));

        TextView stringTvStrCompare = findViewById(R.id.string_tv_strCompare);
        stringTvStrCompare.setText(String.format("字符对比 传入字符 测试数据：测试数据1 > %s\n" +
                                                 "         传入字符 测试数据：测试数据 > %s", stringOpLibProxy.strCompare("测试数据","测试数据1"),stringOpLibProxy.strCompare("测试数据","测试数据")));

        TextView stringTvStrArray = findViewById(R.id.string_tv_strArray);
        stringTvStrArray.setText(String.format("多个字符组合得到字符串数组：传入字符 测试数据*9\n得到数据：%s",
                Arrays.toString(stringOpLibProxy.strArray("测试数据", "测试数据", "测试数据", "测试数据",
                        "测试数据", "测试数据", "测试数据", "测试数据", "测试数据", 9))));


    }

    private void initViewForDynamicRegister() {
        NativeDynamicLibProxy mDynamicLibProxy = new NativeDynamicLibProxy();
        TextView dynamicTvStringStatic = findViewById(R.id.dynamic_tv_string_static);
        dynamicTvStringStatic.setText(String.format("%s%s", dynamicTvStringStatic.getText(), NativeDynamicLibProxy.stringFromDynamicJNIForStatic()));
        TextView dynamicTvString = findViewById(R.id.dynamic_tv_string);
        dynamicTvString.setText(String.format("%s%s", dynamicTvString.getText(), mDynamicLibProxy.stringFromDynamicJNI()));

        TextView dynamicTvShortStatic = findViewById(R.id.dynamic_tv_short_static);
        dynamicTvShortStatic.setText(String.format("%s%s",dynamicTvShortStatic.getText(),NativeDynamicLibProxy.shortFromDynamicJNIForStatic()));
        TextView dynamicTvShort = findViewById(R.id.dynamic_tv_short);
        dynamicTvShort.setText(String.format("%s%s",dynamicTvShort.getText(),mDynamicLibProxy.shortFromDynamicJNI()));

        TextView dynamicTvBooleanStatic = findViewById(R.id.dynamic_tv_boolean_static);
        dynamicTvBooleanStatic.setText(String.format("%s%s",dynamicTvBooleanStatic.getText(),NativeDynamicLibProxy.booleanFromDynamicJNIForStatic()));
        TextView dynamicTvBoolean = findViewById(R.id.dynamic_tv_boolean);
        dynamicTvBoolean.setText(String.format("%s%s",dynamicTvBoolean.getText(),mDynamicLibProxy.booleanFromDynamicJNI()));

        TextView dynamicTvByteStatic = findViewById(R.id.dynamic_tv_byte_static);
        dynamicTvByteStatic.setText(String.format("%s%s",dynamicTvByteStatic.getText(),NativeDynamicLibProxy.byteFromDynamicJNIForStatic()));
        TextView dynamicTvByte = findViewById(R.id.dynamic_tv_byte);
        dynamicTvByte.setText(String.format("%s%s",dynamicTvByte.getText(),mDynamicLibProxy.byteFromDynamicJNI()));

        TextView dynamicTvCharStatic = findViewById(R.id.dynamic_tv_char_static);
        dynamicTvCharStatic.setText(String.format("%s%s",dynamicTvCharStatic.getText(),NativeDynamicLibProxy.charFromDynamicJNIForStatic()));
        TextView dynamicTvChar = findViewById(R.id.dynamic_tv_char);
        dynamicTvChar.setText(String.format("%s%s",dynamicTvChar.getText(),mDynamicLibProxy.charFromDynamicJNI()));

        TextView dynamicTvIntStatic = findViewById(R.id.dynamic_tv_int_static);
        dynamicTvIntStatic.setText(String.format("%s%s",dynamicTvIntStatic.getText(),NativeDynamicLibProxy.intFromDynamicJNIForStatic()));
        TextView dynamicTvInt = findViewById(R.id.dynamic_tv_int);
        dynamicTvInt.setText(String.format("%s%s",dynamicTvInt.getText(),mDynamicLibProxy.intFromDynamicJNI()));

        TextView dynamicTvLongStatic = findViewById(R.id.dynamic_tv_long_static);
        dynamicTvLongStatic.setText(String.format("%s%s",dynamicTvLongStatic.getText(),NativeDynamicLibProxy.longFromDynamicJNIForStatic()));
        TextView dynamicTvLong = findViewById(R.id.dynamic_tv_long);
        dynamicTvLong.setText(String.format("%s%s",dynamicTvLong.getText(),mDynamicLibProxy.longFromDynamicJNI()));

        TextView dynamicTvFloatStatic = findViewById(R.id.dynamic_tv_float_static);
        dynamicTvFloatStatic.setText(String.format("%s%s",dynamicTvFloatStatic.getText(),NativeDynamicLibProxy.floatFromDynamicJNIForStatic()));
        TextView dynamicTvFloat = findViewById(R.id.dynamic_tv_float);
        dynamicTvFloat.setText(String.format("%s%s",dynamicTvFloat.getText(),mDynamicLibProxy.floatFromDynamicJNI()));

        TextView dynamicTvDoubleStatic = findViewById(R.id.dynamic_tv_double_static);
        dynamicTvDoubleStatic.setText(String.format("%s%s",dynamicTvDoubleStatic.getText(),NativeDynamicLibProxy.doubleFromDynamicJNIForStatic()));
        TextView dynamicTvDouble = findViewById(R.id.dynamic_tv_double);
        dynamicTvDouble.setText(String.format("%s%s",dynamicTvDouble.getText(),mDynamicLibProxy.doubleFromDynamicJNI()));
    }

    private void initViewForStaticRegister() {
        NativeStaticLibProxy mStaticLibProxy = new NativeStaticLibProxy();
        TextView staticTvStringStatic = findViewById(R.id.static_tv_string_static);
        staticTvStringStatic.setText(String.format("%s%s", staticTvStringStatic.getText(), NativeStaticLibProxy.stringFromStaticJNIForStatic()));
        TextView staticTvString = findViewById(R.id.static_tv_string);
        staticTvString.setText(String.format("%s%s", staticTvStringStatic.getText(), mStaticLibProxy.stringFromStaticJNI()));

        TextView staticTvShortStatic = findViewById(R.id.static_tv_short_static);
        staticTvShortStatic.setText(String.format("%s%s",staticTvShortStatic.getText(),NativeStaticLibProxy.shortFromStaticJNIForStatic()));
        TextView staticTvShort = findViewById(R.id.static_tv_short);
        staticTvShort.setText(String.format("%s%s",staticTvShort.getText(),mStaticLibProxy.shortFromStaticJNI()));

        TextView staticTvBooleanStatic = findViewById(R.id.static_tv_boolean_static);
        staticTvBooleanStatic.setText(String.format("%s%s",staticTvBooleanStatic.getText(),NativeStaticLibProxy.booleanFromStaticJNIForStatic()));
        TextView staticTvBoolean = findViewById(R.id.static_tv_boolean);
        staticTvBoolean.setText(String.format("%s%s",staticTvBoolean.getText(),mStaticLibProxy.booleanFromStaticJNI()));

        TextView staticTvByteStatic = findViewById(R.id.static_tv_byte_static);
        staticTvByteStatic.setText(String.format("%s%s",staticTvByteStatic.getText(),NativeStaticLibProxy.byteFromStaticJNIForStatic()));
        TextView staticTvByte = findViewById(R.id.static_tv_byte);
        staticTvByte.setText(String.format("%s%s",staticTvByte.getText(),mStaticLibProxy.byteFromStaticJNI()));

        TextView staticTvCharStatic = findViewById(R.id.static_tv_char_static);
        staticTvCharStatic.setText(String.format("%s%s",staticTvCharStatic.getText(),NativeStaticLibProxy.charFromStaticJNIForStatic()));
        TextView staticTvChar = findViewById(R.id.static_tv_char);
        staticTvChar.setText(String.format("%s%s",staticTvChar.getText(),mStaticLibProxy.charFromStaticJNI()));

        TextView staticTvIntStatic = findViewById(R.id.static_tv_int_static);
        staticTvIntStatic.setText(String.format("%s%s",staticTvIntStatic.getText(),NativeStaticLibProxy.intFromStaticJNIForStatic()));
        TextView staticTvInt = findViewById(R.id.static_tv_int);
        staticTvInt.setText(String.format("%s%s",staticTvInt.getText(),mStaticLibProxy.intFromStaticJNI()));

        TextView staticTvLongStatic = findViewById(R.id.static_tv_long_static);
        staticTvLongStatic.setText(String.format("%s%s",staticTvLongStatic.getText(),NativeStaticLibProxy.longFromStaticJNIForStatic()));
        TextView staticTvLong = findViewById(R.id.static_tv_long);
        staticTvLong.setText(String.format("%s%s",staticTvLong.getText(),mStaticLibProxy.longFromStaticJNI()));

        TextView staticTvFloatStatic = findViewById(R.id.static_tv_float_static);
        staticTvFloatStatic.setText(String.format("%s%s",staticTvFloatStatic.getText(),NativeStaticLibProxy.floatFromStaticJNIForStatic()));
        TextView staticTvFloat = findViewById(R.id.static_tv_float);
        staticTvFloat.setText(String.format("%s%s",staticTvFloat.getText(),mStaticLibProxy.floatFromStaticJNI()));

        TextView staticTvDoubleStatic = findViewById(R.id.static_tv_double_static);
        staticTvDoubleStatic.setText(String.format("%s%s",staticTvDoubleStatic.getText(),NativeStaticLibProxy.doubleFromStaticJNIForStatic()));
        TextView staticTvDouble = findViewById(R.id.static_tv_double);
        staticTvDouble.setText(String.format("%s%s",staticTvDouble.getText(),mStaticLibProxy.doubleFromStaticJNI()));
    }

}
